 <?php
class Product extends CActiveRecord {

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function tableName() {
        return "mt_product";
    }
    
    public function attributeLabels() {
        return array(
            "product_category_id" => "กลุ่มสินค้า",
            "product_code" => "รหัสสินค้า",
            "product_name_thai" => "ชื่อสินค้าภาษาไทย",
            "product_name_eng" => "ชื่อสินค้าภาษาอังกฤษ",
            "product_description" => "รายละเอียดสินค้า",
            "product_cost" => "ราคา",
            "product_picture_name" => "รูป",
            "product_status" => "สถานะ",
            "product_create_date" => "วันที่สร้าง",
            "product_modified_date" => "วันที่แก้ไข",
        );
    }
    
    public function rules() {
        return array(
            array("product_code, product_name_eng,product_description,product_cost", "required",'message'=>'กรุณากรอกข้อมูลให้ครบ'),
            array("product_code", "length", "max" => 20),
            array('product_code', 'numerical', 'integerOnly'=>true),
            array("product_name_eng", "length", "max" => 255),
            array("product_description", "length", "max" => 255),
            array("product_cost", "length", "max" => 10),
            array('product_cost', 'numerical', 'integerOnly'=>true)
        );
    }
    
    public function search() {
        $criteria = new CDbCriteria;
        
//        $criteria->compare('product_name_eng',$this->product_name_eng,true);
//
//	$criteria->compare('product_code',$this->product_code);
        
        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }
    
    public function beforeValidate() {
        if ($this->isNewRecord) {
            $this->product_status = 1;
            $this->product_create_date = new CDbExpression("NOW()");
        }
        else
        {
            $this->product_modified_date = new CDbExpression("NOW()");
        }

        return parent::beforeValidate();
    }
}
?>
